//////////Change the directory before running this code; also create a folder named "figures"
//////////Pseudo dataset beta_panel.dta contains 1.8% of the original sample; reg_stats_contests.dta & reg_stats_proposals.dta contain 2.7% of the original sample, respectively.
cd "D:\data&code"

////////////// "Point estimates"

quietly {

/// Proposals regression
use pseudo_dataset/beta_panel, clear
keep if election_group == 1

statsby _b _se num_obs = (e(N)) df = (e(df_r)), ///
by(fund_id_cont) saving(figures/reg_stats_proposals, replace): regress vote_for iss_for_mgmt 

/// Contests regression
use pseudo_dataset/beta_panel, clear
keep if election_group == 2

statsby _b _se num_obs = (e(N)) df = (e(df_r)), ///
by(fund_id_cont) saving(figures/reg_stats_contests, replace): regress vote_for iss_for_mgmt 

/// Combine
use pseudo_dataset/reg_stats_proposals, clear
keep fund_id_cont _b_iss_for_mgmt
rename _b_* prop_beta

tempfile prop
save `prop', replace

use pseudo_dataset/reg_stats_contests, clear
keep fund_id_cont _b_iss_for_mgmt
rename _b_* cont_beta

merge 1:1 fund_id using `prop'
keep if _merge == 3
drop _merge

/// Make deciles of proposals beta
egen prop_dec = xtile(prop_beta), nq(10)

/// Within-decile means
sort prop_dec
by   prop_dec : egen prop_beta_hat = mean(prop_beta)
by   prop_dec : egen cont_beta_hat = mean(cont_beta)

by prop_dec : keep if _n == 1
keep prop_dec *_hat

rename prop_beta_hat prop
rename cont_beta_hat cont

////////////// Save
save figures/point_estimates, replace

}

/////////////// Bootstrap
set seed 17

quietly {

	/////// Count number of contests each fund votes in
	use pseudo_dataset/beta_panel, clear
	keep if election_group == 2

	sort fund_id_cont proposal_id

	by fund_id : gen contest_id = _n 

	save figures/contests_with_ids, replace

	bysort fund_id_cont : gen num_contests = _N
	by     fund_id_cont : keep if _n == 1
	keep fund_id_cont num_contests

	save figures/num_contests, replace

	////// Assign IDs to each proposal within a fund
	use pseudo_dataset/beta_panel, clear
	keep if election_group == 1

	sort fund_id_cont proposal_id

	by fund_id : gen bs_proposal_id = _n 

	save figures/proposals_with_ids, replace

	////// Count number of proposals each fund votes in
	bysort fund_id_cont : gen num_proposals = _N
	by     fund_id_cont : keep if _n == 1
	keep fund_id_cont num_proposals

	save figures/num_proposals, replace

	////// Bootstrap sample
	local ind_bs 1

	while `ind_bs' <= 500 {

		////// Make bootstrap sample
		
		// Draw indexes of proposal (w/replacement)
		use figures/num_contests, clear
		
		merge 1:1 fund_id_cont using figures/num_proposals
		drop _merge
		
		expand num_contests
		
		gen rand_samp = floor((num_proposals)*runiform() + 1)
		sort fund_id_cont rand_samp
		
		rename rand_samp bs_proposal_id
		
		// Merge subsample of proposals in
		merge m:1 fund_id_cont bs_proposal_id using figures/proposals_with_ids
		keep if _merge == 3
		drop _merge
		
		////// Run regressions
		keep fund_id_cont vote_for iss_for_mgmt
		
		statsby _b _se num_obs = (e(N)) df = (e(df_r)), ///
		by(fund_id_cont) saving(figures/reg_stats_proposals_`ind_bs', replace): regress vote_for iss_for_mgmt 

		////// Combine true proposal betas with bootstrapped subsample proposal betas
		use pseudo_dataset/reg_stats_proposals, clear
		keep fund_id_cont _b_iss_for_mgmt
		rename _b_* true_prop_beta

		tempfile prop
		save `prop', replace

		use figures/reg_stats_proposals_`ind_bs', clear
		keep fund_id_cont _b_iss_for_mgmt
		rename _b_* bs_prop_beta

		merge 1:1 fund_id using `prop'
		keep if _merge == 3
		drop _merge

		/// Make deciles of proposals beta
		egen prop_dec = xtile(true_prop_beta), nq(10)

		/// Within-decile means
		sort prop_dec
		by   prop_dec : egen true_prop_beta_hat = mean(true_prop_beta)
		by   prop_dec : egen bs_prop_beta_hat   = mean(bs_prop_beta)
		
		by prop_dec : keep if _n == 1
		keep prop_dec *_hat
		
		rename true_prop_beta_hat true_prop
		rename bs_prop_beta_hat   bs_prop	

		/// Save 
		gen ind_bs = `ind_bs'
		save figures/deciles_`ind_bs', replace
		
		/// Increment
		local ind_bs = `ind_bs' + 1
		
	}

	///////////// Take quantiles of deciles
	clear
	cd figures/

	append using `: dir . files "deciles_*.dta"'

	bysort prop_dec : egen LB = pctile(bs_prop), p(2.5)
	by	   prop_dec : egen UB = pctile(bs_prop), p(97.5)

	by prop_dec : keep if _n == 1
	keep prop_dec true_prop bs_prop LB UB

	///////////// Save
	cd ../../../../
	keep prop_dec LB UB
	save figures, replace

}
